package com.lvhr.example.consumer;

import com.lvhr.example.common.model.User;
import com.lvhr.example.common.service.UserService;
import com.lvhr.rpc.RpcApplication;
import com.lvhr.rpc.config.RpcConfig;
import com.lvhr.rpc.constant.RpcConstant;
import com.lvhr.rpc.proxy.ServiceProxyFactory;
import com.lvhr.rpc.utils.ConfigUtils;

import java.util.Collections;

/**
 * 简易mock服务消费者示例
 */
public class ConsumerMockExample {
    public static void main(String[] args) {
        RpcConfig rpc = ConfigUtils.loadConfig(RpcConfig.class,"rpc","", Collections.singletonList("properties"));
        RpcApplication.init(rpc);
        UserService userService;
        if (rpc.isMock()){
            userService = ServiceProxyFactory.getMockProxy(UserService.class);
            long number = userService.getNumber();
            System.out.println(number);
        }
        System.out.println(rpc.toString());

        // 获取代理
         userService = ServiceProxyFactory.getProxy(UserService.class);
        User user = new User();
        user.setName("lv-rpc");
        // 调用
        User newUser = userService.getUser(user);
        if (newUser != null) {
            System.out.println(newUser.getName());
        } else {
            System.out.println("user == null");
        }
        long number = userService.getNumber();
        System.out.println(number);
    }

}
